 new Vue({
    el: '#app',     
    data() {
      var validateName = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请输入用户名'));
        }else{
        	return callback();
        }
      };
      var validatePass = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请输入密码'));
        }else{
        	return callback();
        }
      };
      return {
    	sys_name:'',
    	rememberMe:true, 
    	callname:'',
    	banquan:'',
        ruleForm: {
          userName: '',
          pwd: '',
          ip:'',
		  source:'web'
        },
        rules: {
           userName: [
		     { validator: validateName, trigger: 'blur' }
		  ],
		  pwd: [
            { validator: validatePass, trigger: 'blur' }
          ]          
        }
      };
    },
    created(){
        this.convert();
        this.iphelp();
        this.localStoragefun();
        //enter登录
        var t = this;
        document.onkeydown = function(e){
          if(window.event == undefined){
            var key = e.keyCode;
          }else{
            var key = window.event.keyCode;
          }
        //enter的ASCII码是13
          if(key == 13){
            t.submitForm('ruleForm');
          }
        }        
    },
    methods: {
      localStoragefun:function(){
    	  if(window.localStorage.getItem('userName')||window.localStorage.getItem('userName')!=null){    		  
    		  this.ruleForm.userName=window.localStorage.getItem('userName');
          }else{
        	  this.ruleForm.userName=''
          }
          if(window.localStorage.getItem('pwd')||window.localStorage.getItem('pwd')!=null){
        	  this.ruleForm.pwd=window.localStorage.getItem('pwd');
          }
          
      },	
	  convert: function () {
		  axios({
                  method: "post",
                  url: '/sys/getSys.do',
                  headers: {"token" : "", "isClient":"web",'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
              }).then((data) => { 
            	   var res=data.data
            	   if(res.status == 200){
    		           this.sys_name=res.resultData.sys.sysName;
    		           this.callname=res.resultData.sys.phone;
    		           this.banquan=res.resultData.sys.copyright;
	           	   }else{
	           		   this.$message({
	                          message: '信息获失败',
	                          type: 'warning'
	                      });
	           	   }
		      }).catch((err) => {		    	 
		    	  this.$message({
                      message: '系统信息获失败',
                      type: 'warning'
                    });
             });
      },
      //获取登录地址
      iphelp:function(){
    	     axios({
    	    	  method:'get',
	              url: 'https://ip.help.bj.cn/'
               }).then((data) => { 
            	   if(data.status == 200){
            		   this.ruleForm.ip=data.data.data[0]["ip"]+","+data.data.data[0]["nation"]+data.data.data[0]["province"]+data.data.data[0]["city"]+data.data.data[0]["district"]+'(后台)'            		          	          
            	   }else{
            		   this.$message({
                           message: '信息获失败',
                           type: 'warning'
                       });
            	   }
               }).catch((err) => {
		    	  this.$message({
                    message: '信息获失败',
                    type: 'warning'
                  });
           });
      },
      submitForm(formName) {
    	var vm = this;
        this.$refs[formName].validate((valid) => {
            var params = new URLSearchParams();
            params.append('userName', this.ruleForm.userName);
            params.append('pwd', this.ruleForm.pwd);
            params.append('ip', this.ruleForm.ip);
            params.append('source', this.ruleForm.source);
          if (valid) {//验证成功
        	  axios({
                  method: "post",
                  url: '/login.do',
                  headers: {"token" : "", "isClient":"web",'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                  data: params
              })
              .then(function(res) {
            	  var Datanum=res.data
            	  if(Datanum.status == 200){
            		  vm.$message({
                          message: Datanum.resultMsg,
                          type: 'success'
                        });
                	  if(! window.localStorage){
              	        layer.msg("浏览器不支持localstorage", {icon: 5});
    	        		    return false; 
    	        	  }else{
    	        		  if(vm.rememberMe === true){//记住用户密码
    	        			//存储用户信息
    		        		  window.localStorage.setItem('userName',vm.ruleForm.userName);
    						  window.localStorage.setItem('pwd',vm.ruleForm.pwd);
    	        		  }else{//清楚数据
    	        			  window.localStorage.removeItem("userName");
    						  window.localStorage.removeItem('pwd','');
    	        		  }	
    	        		  
    	        	  }
                      //window.location="/res/page/index.html"; 
                	  window.location= "/res/page/main.html"
            	  }else{//登录三次
            		  if(Datanum.success===false){
            				$(".resultmsg").show();
                          	$(".msg").html(Datanum.resultMsg);
                      		$(".msgtime").show();
                      		$(".login_btn").text("(60s)重新尝试登录");
                      		$(".login_btn").attr("disabled",true);
                      		var time=60;
                              setTime=setInterval(function(){
                                  if(time<=0){
                                      clearInterval(setTime);
                                      $(".resultmsg").hide();
                                      $(".msg").html("");
                                      $(".login_btn").attr("disabled",false);
                                      $(".login_btn").html("登&nbsp;&nbsp;录");
                                      return;
                                  }
                                  time--;
                                  $(".login_btn").text("("+time+"s)重新尝试登录");
                              },1000);
            		  }else{
            			  vm.$message({
                              message: Datanum.resultMsg,
                              type: 'warning'
                           });
            		  }
                                      
            	  }            	            	  
             })
          } else {
        	  vm.$message({
                  message: '信息错误',
                  type: 'warning'
               });
            return false;
          }
        });
      }
    }  
 })